// Show lines M through N of stdin. If M is ommitted, M=0. If N is
// ommitted, read till end of stdin.
package main

import (
	"os"
	"log"
	"bufio"
	"strconv"
	"strings"
)

func usage(msg string) {
	if len(msg) != 0 {
		log.Println(msg)
	}
	log.Println("Usage: slice M-N")
	log.Println("Where M is the first line and N is the last.")
	log.Fatalln("Either may be omitted, meaning from the first",
		"line or to the last line, respectively.")
}

func atop(a string, d int) int {
	if a != "" {
		i, err := strconv.Atoi(a)
		if err != nil || i < 0 {
			usage(a + " is an invalid number")
		}
		return i - 1
	}
	return d
}

//Usage: %name M-N
func main() {
	log.SetFlags(0)
	if len(os.Args) != 2 {
		usage("No line numbers specified")
	}

	arg := strings.Split(os.Args[1], "-", 2)
	if len(arg) != 2 || (arg[0] == "" && arg[1] == "") {
		usage("Invalid range specified: " + os.Args[1])
	}
	m, n := atop(arg[0], 0), atop(arg[1], -1)
	if n > 0 && n < m {
		usage("Range swapped")
	}

	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	for lno := 0; n < 0 || lno <= n; lno++ {
		line, err := in.ReadSlice('\n')

		if lno >= m {
			out.Write(line)
		}

		if err != nil {
			break
		}
	}

	out.Flush()
}
